package com.miyake.client.admin;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.profile.UserElement;
import com.miyake.client.profile.UserProperty;
import com.miyake.client.tools.ImageListElement;

public class UserManagement extends Composite {
	protected DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();
	private VerticalPanel listPanel;
	
	public UserManagement() {
		
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		
		Label lblNewLabel = new Label("Email");
		horizontalPanel.add(lblNewLabel);
		
		final TextBox textBox = new TextBox();
		horizontalPanel.add(textBox);
		
		Button btnNewButton = new Button("Add");
		btnNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addUser(textBox.getText());
			}
		});
		horizontalPanel.add(btnNewButton);
		listPanel = new VerticalPanel();
		verticalPanel.add(listPanel);
		getUserList();
	}

	private void getUserList() {
		listPanel.clear();
		final ListBox list = new ListBox();
		list.setVisibleItemCount(10);
		listPanel.add(list);
		listPanel.add(new Button("Delete", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				deleteUser(list.getItemText(list.getSelectedIndex()));
			}
		}));
		listPanel.add(new Button("Get", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getUser(list.getItemText(list.getSelectedIndex()));
			}
			
		}));
		
		service.getAllUsers(new AsyncCallback<List<ImageListElement>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<ImageListElement> result) {
				for (ImageListElement user : result) {
					list.addItem(user.getKey());
				}
			}
			
		});
	}

	protected void getUser(String email) {
		service.getUser(email, new AsyncCallback<UserProperty>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(UserProperty result) {
				for (UserElement element : result.getProperties()) {
					listPanel.add(new Label(element.getTitle()));
				}
			}
		});
	}

	protected void deleteUser(String email) {
		service.deleteUser(email, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				getUserList();
			}
		});
	}

	protected void addUser(String text) {
		service.addUser(text, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				getUserList();
			}
		});
	}

}
